﻿/*
 * File Authors:
 * 		Alfonso Martínez de Lizarrondo
 *
 * Based on fck_link.js
 * 
 */

var oEditor		= window.parent.InnerDialogLoaded() ;
var FCK			= oEditor.FCK ;
var FCKLang		= oEditor.FCKLang ;
var FCKConfig	= oEditor.FCKConfig ;


//#### Regular Expressions library.
var oRegex = new Object() ;

//adjusted to allow an url starting with / as a good one
oRegex.UriProtocol = new RegExp('') ;
oRegex.UriProtocol.compile( '^(((http|https|ftp|news):\/\/)|mailto:|\/)', 'gi' ) ;



//#### Initialization Code

// oLink: The actual selected link in the editor.
var oLink = FCK.Selection.MoveToAncestorNode( 'A' ) ;
if ( oLink )
	FCK.Selection.SelectNode( oLink ) ;

window.onload = function()
{
	// Translate the dialog box texts.
	oEditor.FCKLanguageManager.TranslatePage(document) ;

	// Load the selected link information (if any).
	LoadSelection() ;

	GetE('txtUrl').focus();
	window.parent.SetAutoSize( true ) ;

	// Activate the "OK" button.
	window.parent.SetOkButton( true ) ;
}


function LoadSelection()
{
	if ( !oLink ) return ;

	// Get the actual Link href.
	var sHRef = oLink.getAttribute( '_fcksavedurl' ) ;
	if ( !sHRef || sHRef.length == 0 )
		sHRef = oLink.getAttribute( 'href' , 2 ) + '' ;
	

	GetE('txtUrl').value = sHRef ;

//	GetE('txtAttTitle').value		= oLink.title ;
}


//#### The OK button was hit.
function Ok()
{
	var sUri, sInnerHtml ;

	sUri = GetE('txtUrl').value ;

	if ( sUri.length == 0 )
	{
		alert( FCKLang.DlnLnkMsgNoUrl ) ;
		return false ;
	}

	//check that the links have a proper protocol or assume http
	var sProtocol = oRegex.UriProtocol.exec( sUri ) ;
	if ( !sProtocol )
	{
		sUri = "http://" + sUri ;
	}

	// No link selected, so try to create one.
	if ( !oLink )
		oLink = oEditor.FCK.CreateLink( sUri ) ;
	
	if ( oLink )
		sInnerHtml = oLink.innerHTML ;		// Save the innerHTML (IE changes it if it is like a URL).
	else
	{
		// If no selection, use the uri as the link text (by dom, 2006-05-26)
		sInnerHtml = sUri;

		var oLinkPathRegEx = new RegExp("//?([^?\"']+)([?].*)?$");
		var asLinkPath = oLinkPathRegEx.exec( sUri );
		if (asLinkPath != null)
			sInnerHtml = asLinkPath[1];  // use matched path


		// built new anchor and add link text
		oLink = oEditor.FCK.CreateElement( 'a' ) ;
	}
	
	oEditor.FCKUndo.SaveUndoStep() ;

	oLink.href = sUri ;
	SetAttribute( oLink, '_fcksavedurl', sUri ) ;

	oLink.innerHTML = sInnerHtml ;		// Set (or restore) the innerHTML

//		SetAttribute( oLink, 'target', null ) ;
//	SetAttribute( oLink, 'title'	, GetE('txtAttTitle').value ) ;

	// Select the link.
	oEditor.FCKSelection.SelectNode(oLink);
	
	return true ;
}


function SetUrl( url )
{
	document.getElementById('txtUrl').value = url ;
}


